package aufgabe_01;

import java.io.*;

public class Filter {
	public static void main(String args[]) {
		if (args.length != 3) {
			System.err.println("Bitte 3 Parameter: <Suchmuster> <Dateiname>");
			System.exit(1);
		}
		String patt = args[0];
		String fileIn = args[1];
		String fileOut = args[2];
		try {
			LineNumberReader reader = new LineNumberReader(new FileReader(fileIn));
			BufferedOutputStream out = new BufferedOutputStream (System.out, 100);
			PrintStream print = new PrintStream (out);
			FileWriter fw = new FileWriter(fileOut);			
			String str;
			while ((str = reader.readLine()) != null) {
				if (str.indexOf(patt) != -1) {
					int ln = reader.getLineNumber();
					System.out.println(fileIn + "[" + ln + "]: " + str );					
					fw.write(fileIn + "[" + ln + "]: " + str + "\n");
				}
			}
			reader.close();			
			fw.close();
		} catch (IOException e) {
			System.err.println("IO-Fehler beim Filtern");
			e.printStackTrace();
		}
	}
}